package com.talking.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.talking.R;
import com.talking.bean.FriendOrGroupBean;
import com.talking.databinding.ItemFriendBinding;
import com.talking.model.Http;
import com.talking.presenter.UserPresenter;
import com.talking.utils.Utils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class FriendOrGroupAdapter extends ArrayAdapter<FriendOrGroupBean> {
    private static final int layoutId = R.layout.item_friend;
    private final Activity context;
    public FriendOrGroupAdapter(Activity context, List<FriendOrGroupBean> objects){
        super(context,layoutId,objects);
        this.context=context;
    }
    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        FriendOrGroupBean friendOrGroupBean = getItem(position);
        @SuppressLint("ViewHolder") @NonNull ItemFriendBinding binding = ItemFriendBinding.inflate(LayoutInflater.from(context));
        if (friendOrGroupBean.type.equals("friend")) {
            binding.itemSearchAccount.setText("账号:" + friendOrGroupBean.friendAccount);
            binding.itemSearchNickname.setText(friendOrGroupBean.remark);
            UserPresenter.getUserByAccount(friendOrGroupBean.friendAccount, null, new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    e.printStackTrace();
                }
                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) {
                    context.runOnUiThread(() -> {
                        try {
                            JSONObject jo = new JSONObject(Objects.requireNonNull(response.body()).string());
                            Glide.with(context).load(Http.BASE_URL + jo.getString("iconUrl")).apply(Utils.getGlideOption()).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(binding.itemSearchIcon);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                }
            });
        }
        if (friendOrGroupBean.type.equals("group")){
            binding.itemSearchAccount.setText("群ID:"+friendOrGroupBean.groupId);
            binding.itemSearchNickname.setText(friendOrGroupBean.groupname);
            Glide.with(context).load(Http.BASE_URL + friendOrGroupBean.groupIcon).apply(Utils.getGlideOption()).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(binding.itemSearchIcon);
        }
        return binding.getRoot();
    }
}